package com.fenrirtec.aepp.common.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fenrirtec.aepp.common.condition.NewsNoticeCondition;
import com.fenrirtec.aepp.common.dao.NewsNoticeDao;
import com.fenrirtec.aepp.common.dao.impl.NewsNoticeDao_JDBCImpl;
import com.fenrirtec.aepp.common.dto.NewsNoticeDto;
import com.fenrirtec.aepp.common.model.NewsNotice;
import com.fenrirtec.aepp.common.service.NewsNoticeService;
import com.fenrirtec.core.database.DatabaseSessionManager;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.BeanCopierUtils;

public class NewsNoticeServiceImpl implements NewsNoticeService {
	
	private static final Logger logger = LoggerFactory.getLogger(NewsNoticeServiceImpl.class);

	@Override
	public List<NewsNoticeDto> searchByCategory(Integer category) {
		if (logger.isInfoEnabled()) {
			logger.info("[NewsNoticeServiceImpl#findNewsNoticeList] start.");
		}
		
		DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
		sessionManager.open();
		Connection connection = sessionManager.getSession();

		List<NewsNotice> newsNotices = null;
		
		try {
			connection.setAutoCommit(false);
			NewsNoticeDao newsNoticeDao = new NewsNoticeDao_JDBCImpl();
			newsNotices = newsNoticeDao.searchByCategory(category);
			//connection.commit();
		} catch (DatabaseException e) {
			// expected
			if (logger.isErrorEnabled()) {
				logger.error("[NewsNoticeServiceImpl#findNewsNoticeList] database error occurred.", e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw e;
		} catch (Exception e) {
			// unexpected
			if (logger.isErrorEnabled()) {
				logger.error("[NewsNoticeServiceImpl#findNewsNoticeList] unexpected error occurred.", e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw new UnexpectedException("unexpected error.", e);
		} finally {
			if (logger.isInfoEnabled()) {
				logger.info("[NewsNoticeServiceImpl#findNewsNoticeList] finish.");
			}
			sessionManager.close();
		}

		List<NewsNoticeDto> newsNoticeDtos = new ArrayList<NewsNoticeDto>();
		if (newsNotices != null && newsNotices.size() > 0) {
			for (NewsNotice newsNotice : newsNotices) {
				NewsNoticeDto newsNoticeDto = new NewsNoticeDto();
				BeanCopierUtils.copyProperties(newsNotice, newsNoticeDto);
				newsNoticeDtos.add(newsNoticeDto);
			}
		}
		return newsNoticeDtos;
	}

	@Override
	public List<NewsNoticeDto> searchByCnd(NewsNoticeCondition condition) {
		if (logger.isInfoEnabled()) {
			logger.info("[NewsNoticeServiceImpl#searchByCnd] start.");
		}
		
		DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
		sessionManager.open();
		Connection connection = sessionManager.getSession();

		List<NewsNotice> newsNoticeList = null;
		
		try {
			connection.setAutoCommit(false);
			NewsNoticeDao newsNoticeDao = new NewsNoticeDao_JDBCImpl();
			newsNoticeList = newsNoticeDao.searchByCnd(condition);
		} catch (DatabaseException e) {
			// expected
			if (logger.isErrorEnabled()) {
				logger.error("[NewsNoticeServiceImpl#searchByCnd] database error occurred.", e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw e;
		} catch (Exception e) {
			// unexpected
			if (logger.isErrorEnabled()) {
				logger.error("[NewsNoticeServiceImpl#searchByCnd] unexpected error occurred.", e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw new UnexpectedException("unexpected error.", e);
		} finally {
			if (logger.isInfoEnabled()) {
				logger.info("[NewsNoticeServiceImpl#searchByCnd] finish.");
			}
			sessionManager.close();
		}

		List<NewsNoticeDto> newsNoticeDtoList = new ArrayList<NewsNoticeDto>();
		if (newsNoticeList != null && newsNoticeList.size() > 0) {
			for (NewsNotice newsNotice : newsNoticeList) {
				NewsNoticeDto newsNoticeDto = new NewsNoticeDto();
				BeanCopierUtils.copyProperties(newsNotice, newsNoticeDto);
				newsNoticeDtoList.add(newsNoticeDto);
			}
		}
		return newsNoticeDtoList;
	}

    @Override
    public Integer countByCnd(NewsNoticeCondition condition) {
        if (logger.isInfoEnabled()) {
            logger.info("[NewsNoticeServiceImpl#countByCnd] start.");
        }
        
        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();

        try {
            connection.setAutoCommit(false);
            NewsNoticeDao newsNoticeDao = new NewsNoticeDao_JDBCImpl();
            Integer newsNoticeCount = newsNoticeDao.countByCnd(condition);
            return newsNoticeCount;
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#countByCnd] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#countByCnd] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[NewsNoticeServiceImpl#countByCnd] finish.");
            }
            sessionManager.close();
        }
    }

    @Override
    public Boolean publish(NewsNoticeDto newsNoticeDto) {
        if (logger.isInfoEnabled()) {
            logger.info("[NewsNoticeServiceImpl#publish] start.");
        }
        
        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            NewsNoticeDao newsNoticeDao = new NewsNoticeDao_JDBCImpl();
            NewsNotice newsNotice = new NewsNotice();
            BeanCopierUtils.copyProperties(newsNoticeDto, newsNotice);
            result = newsNoticeDao.insert(newsNotice);
            if (result) {
                connection.commit();
            } else {
                connection.rollback();
            }
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#publish] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#publish] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[NewsNoticeServiceImpl#publish] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public Boolean edit(NewsNoticeDto newsNoticeDto) {
        if (logger.isInfoEnabled()) {
            logger.info("[NewsNoticeServiceImpl#edit] start.");
        }
        
        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            NewsNoticeDao newsNoticeDao = new NewsNoticeDao_JDBCImpl();
            NewsNotice newsNotice = new NewsNotice();
            BeanCopierUtils.copyProperties(newsNoticeDto, newsNotice);
            result = newsNoticeDao.update(newsNotice);
            if (result) {
                connection.commit();
            } else {
                connection.rollback();
            }
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#edit] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#edit] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[NewsNoticeServiceImpl#edit] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public Boolean remove(Integer newsNoticeId) {
        if (logger.isInfoEnabled()) {
            logger.info("[NewsNoticeServiceImpl#remove] start.");
        }
        
        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            NewsNoticeDao newsNoticeDao = new NewsNoticeDao_JDBCImpl();
            result = newsNoticeDao.delete(newsNoticeId);
            if (result) {
                connection.commit();
            } else {
                connection.rollback();
            }
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#remove] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#remove] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[NewsNoticeServiceImpl#remove] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public NewsNoticeDto findDetail(Integer newsNoticeId) {
        if (logger.isInfoEnabled()) {
            logger.info("[NewsNoticeServiceImpl#findDetail] start.");
        }
        
        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();

        NewsNotice newsNotice = null;
        
        try {
            connection.setAutoCommit(false);
            NewsNoticeDao newsNoticeDao = new NewsNoticeDao_JDBCImpl();
            newsNotice = newsNoticeDao.findDetail(newsNoticeId);
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#findDetail] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[NewsNoticeServiceImpl#findDetail] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[NewsNoticeServiceImpl#findDetail] finish.");
            }
            sessionManager.close();
        }

        NewsNoticeDto newsNoticeDto = null;
        if (newsNotice != null) {
            newsNoticeDto = new NewsNoticeDto();
            BeanCopierUtils.copyProperties(newsNotice, newsNoticeDto);
        }
        return newsNoticeDto;
    }

}
